package com.xyb.service;

import com.xyb.dao.BaseFileDao;
import com.xyb.entity.BaseFile;
import com.xyb.entity.FileId;
import com.xyb.utils.FileNameUtils;
import com.xyb.utils.IdUtils;
import com.xyb.utils.OSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FileService {
    @Resource
    private IdUtils idUtils;
    @Resource
    private BaseFileDao baseFileDao;

    /**
     * 单文件上传
     *
     * @param file
     * @return
     */
    public Long insertFile(MultipartFile file) {
        long nextId = idUtils.nextId();
        long bizId = idUtils.nextId();

        String filename = file.getOriginalFilename();
        String suffix = FileNameUtils.getSuffix(filename);
        String tmpName = nextId + "." + suffix;
        log.info("文件名: " + tmpName);

        String url = "";
        try {
            url = OSSUtils.upload(tmpName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        BaseFile baseFile = BaseFile.builder()
                .fileId(nextId)
                .bizId(bizId)
                .fileName(filename)
                .prefixName(FileNameUtils.getPrefix(filename))
                .suffixName(suffix)
                .fileType(file.getContentType())
                .fileSize(file.getSize())
                .urlName(tmpName)
                .url(url)
                .status(1)
                .showOrder(0L)
                .createTime(new Date())
                .build();
        baseFileDao.insertBaseFile(baseFile);
        return nextId;
    }

    /**
     * @param files
     * @return
     */
    public FileId insertFiles(List<MultipartFile> files) {
        long bizId = idUtils.nextId();
        List<Long> fileIds = new ArrayList<>();
        files.forEach(file -> {
            long nextId = idUtils.nextId();
            uploadFiles(nextId,bizId,file);
            fileIds.add(nextId);
        });
        FileId fileId = FileId.builder()
                .bizId(bizId)
                .fileIds(fileIds)
                .build();
        return fileId;
    }

    @Async
    public void uploadFiles(long fileId,long bizId,MultipartFile file){
        String filename = file.getOriginalFilename();
        String suffix = FileNameUtils.getSuffix(filename);
        String tmpName = fileId + "." + suffix;
        String url = "";
        try {
            url = OSSUtils.upload(tmpName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        BaseFile baseFile = BaseFile.builder()
                .fileId(fileId)
                .bizId(bizId)
                .fileName(filename)
                .prefixName(FileNameUtils.getPrefix(filename))
                .suffixName(suffix)
                .fileType(file.getContentType())
                .fileSize(file.getSize())
                .urlName(tmpName)
                .url(url)
                .status(1)
                .createTime(new Date())
                .build();
        baseFileDao.insertBaseFile(baseFile);
    }
}
















































